package Dao;

import Bean.Administrator;
import Bean.Audit;
import Utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 张
 * Date: 2025-03-09
 * Time: 12:18
 */
//管理员
public class AdministratorDao {
    private Connection connection = null;
    private PreparedStatement statement = null;
    private ResultSet resultSet = null;

    //1.新增 --- 只能系统操作这个类
    public boolean insert(Administrator administrator) {
        try {
            //1.和数据库建立连接
            connection = DBUtil.getConnection();
            //2.构造sql语句
            String sql = "insert into CREATE TABLE Administrator values(?,?,?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1,administrator.getAdministratorId());
            statement.setString(2,administrator.getUsername());
            statement.setString(3,administrator.getPassword());
            //3.执行sql
            int ret = statement.executeUpdate();
            if(ret == 1) {
                return true;
            } else {
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,null);
        }
        return false;
    }

    //2.查询管理员
    public Administrator selectByName(String name) {
        Administrator administrator = null;
        try {
            //1.和数据库建立连接
            connection = DBUtil.getConnection();
            //2.构造sql语句
            String sql = "select * from Administrator where user_name = ?";
            statement = connection.prepareStatement(sql);
            statement.setString(1,name);
            resultSet = statement.executeQuery();
            if (resultSet.next()) {
                administrator = new Administrator();
                administrator.setAdministratorId(resultSet.getString("administrator_id"));
                administrator.setUsername(resultSet.getString("user_name"));
                administrator.setPassword(resultSet.getString("password"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return administrator;
    }
}
